package com.hdoit.ares.web.home.apis;

import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.selfcommon.enums.api.AppResponseStatus;
import com.tennetcn.common.logical.app.model.LoginAuthority;
import com.tennetcn.common.logical.app.service.ILoginAuthorityService;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;
import com.tennetcn.web.cbase.ApiAuthPassport;

/** 
 * @author      x
 * @email       xqxbbe@qq.com
 * @createtime  2018年1月29日 下午2:40:20
 * @comment 
 */

public abstract class HomeAresApi extends BaseApi{
	
	protected ICustomerService customerService;

	public ICustomerService getCustomerService() {
		return customerService;
	}

	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	protected ILoginAuthorityService loginAuthorityService;

	public ILoginAuthorityService getLoginAuthorityService() {
		return loginAuthorityService;
	}

	@Resource
	public void setLoginAuthorityService(
			ILoginAuthorityService loginAuthorityService) {
		this.loginAuthorityService = loginAuthorityService;
	}
	
	
	private final String loginApi = "login";
	private final String loginTestApi = "loginTest";
	private final String testApi = "test";
	
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = loginApi)
	@ApiOperation(notes = loginApi,httpMethod = "POST",value = "登陆")
	public String login(String userName,String password,String deviceId,String deviceName){
		
		return logining(userName,password,deviceId,deviceName);
	}
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = loginTestApi)
	@ApiOperation(notes = loginTestApi, httpMethod = "POST", value = "登陆测试")
	public String loginTest(String userName,String password){
		return logining(userName,password,CommonUtils.getKeyId(),"内部测试");
	}
	
	@ResponseBody
	@RequestMapping(value = testApi)
	@ApiOperation(notes = testApi, httpMethod = "POST", value = "测试登陆访问")
	public String test(){
		return new ResponseModel(ResponseStatus.success,"访问成功").toJson();
	}
	
	
	
	private String logining(String userId,String password,String deviceId,String deviceName){
		ResponseModel rm = new ResponseModel();
		Customer customer = customerService.login(userId, password);
		if(customer==null){
			rm.setStatus(ResponseStatus.dataError);
			rm.setMessage("用户名或密码错误");
		}else{	
			LoginAuthority loginAuthority = loginAuthorityService.saveLoginAuthority(customer.getId(),customer.getUsername(), deviceId, deviceName);
			if(loginAuthority == null){
				rm.setStatus(ResponseStatus.dataError);
				rm.setMessage("授权出现异常，请与管理员联系");
			}else{
				List<String> params = new ArrayList<String>();
				params.add("id");
				params.add("username");
				params.add("nickname");
				params.add("userIdentity");
				params.add("sex");
				params.add("phone");
				params.add("refereeId");
				params.add("refereeIdentity");
				params.add("region");
				params.add("level");
				params.add("picPath");
				
				rm.setStatus(AppResponseStatus.login_seccess);
				rm.setMessage("登录成功");
				rm.put("signCode", loginAuthority.getSignCode());
				rm.put("expire",loginAuthority.getExpireDate());
				rm.put("password", password);
				rm.putObj("customer",customer,params);
			}
		}
		return rm.toJson();
	}

}

